package net.sf.calibur.transport;

import net.sf.calibur.msgbuf.MessageBean;
import net.sf.calibur.engine.Event;
import net.sf.calibur.transport.message.CommandMessage;

public class CommandMessageEvent extends Event {
	private CommandMessage commandMessage;
	private Session session;

	private long beginHandleMillis;
	private long endHandleMillis;

	public CommandMessageEvent(CommandMessage cm, Session session) {
		this.commandMessage = cm;
		this.session = session;
	}
	
	@Override
	public void release() {
		commandMessage.release();
	}

	@Override
	public Object getId() {
		return new Integer(commandMessage.getCommandId());
	}

	public Session getSession() {
		return session;
	}

	public void setSession(Session session) {
		this.session = session;
	}

	public long getBeginHandleMillis() {
		return beginHandleMillis;
	}

	public void setBeginHandleMillis() {
		this.beginHandleMillis = System.currentTimeMillis();
	}

	public long getEndHandleMillis() {
		return endHandleMillis;
	}

	public void setEndHandleMillis() {
		this.endHandleMillis = System.currentTimeMillis();
	}

	public long getArrivedMillis() {
		return commandMessage.getArrivedTimestamp();
	}

	public int getCommandId() {
		return commandMessage.getCommandId();
	}

	public MessageBean getInput() {
		return commandMessage.getCommand().getInput().getMessageBean();
	}
	
	public MessageBean getOutput() {
		return commandMessage.getCommand().getOutput().getMessageBean();
	}
	
	public void sendResponse() throws Exception {
		session.sendCommandResponse(commandMessage);
	}

	@Override
	public String toString() {
		if (commandMessage == null) {
			return "null";
		} else {
			return commandMessage.toString();
		}
	}

	public CommandMessage getCommandMessage() {
		return commandMessage;
	}

	public void setCommandMessage(CommandMessage commandMessage) {
		this.commandMessage = commandMessage;
	}

	public void setBeginHandleMillis(long beginHandleMillis) {
		this.beginHandleMillis = beginHandleMillis;
	}

	public void setEndHandleMillis(long endHandleMillis) {
		this.endHandleMillis = endHandleMillis;
	}
}
